local meta = FindMetaTable( "Entity" )

function meta:MakeSmoke()
	if (self.EnergyDamageEffects && self.EnergyDamageEffects > 0) then return end --dont do smoke if energy is sparking
	if ((self.SmokeDamageEffects or 0) > 1) then return end

	self.SmokeDamageEffects = (self.SmokeDamageEffects or 0) + 1

	timer.Simple(1.5, function(self)
		self.SmokeDamageEffects = self.SmokeDamageEffects - 1
	end, self)

	local Smoke = ents.Create("env_smoketrail")
	Smoke:SetKeyValue("opacity", 1)
	Smoke:SetKeyValue("spawnrate", 10)
	Smoke:SetKeyValue("lifetime", 2)
	Smoke:SetKeyValue("startcolor", "180 180 180")
	Smoke:SetKeyValue("endcolor", "255 255 255")
	Smoke:SetKeyValue("minspeed", 15)
	Smoke:SetKeyValue("maxspeed", 30)
	Smoke:SetKeyValue("startsize", (self.Entity:BoundingRadius() / 2))
	Smoke:SetKeyValue("endsize", self.Entity:BoundingRadius())
	Smoke:SetKeyValue("spawnradius", 10)
	Smoke:SetKeyValue("emittime", 300)
	Smoke:SetKeyValue("firesprite", "sprites/firetrail.spr")
	Smoke:SetKeyValue("smokesprite", "sprites/whitepuff.spr")
	Smoke:SetPos(self.Entity:GetPos())
	Smoke:SetParent(self.Entity)
	Smoke:Spawn()
	Smoke:Activate()
	Smoke:Fire("kill","", 1)
end



function meta:CreateWaterEffect( pos )
	if (!self.WaterEffects) then self.WaterEffects = 0 end

	--make 10 water effects per ent
	if (self.WaterEffects > 3) then return end

	self.WaterEffects = self.WaterEffects + 1

	local waterEnt = ents.Create("water_projectile")
	waterEnt:SetPos(pos)
	waterEnt:Spawn()
	waterEnt:SetNetworkedInt("r", 100)
	waterEnt:SetNetworkedInt("g", 100)
	waterEnt:SetNetworkedInt("b", 255)
	waterEnt:SetNetworkedInt("a", 80)
	waterEnt:SetNetworkedInt("viscosity", 5)
	waterEnt:SetCollisionGroup( 4 )

	function waterEnt:OnRemove()
		self.WaterEffects = self.WaterEffects - 1
	end

	timer.Simple( 2, function()
		if (waterEnt && waterEnt:IsValid()) then
			waterEnt:Remove()
		end
	end, self, waterEnt)
end

function meta:EnergyDamage( pos, mag )
	if ((self.EnergyDamageEffects or 0) > 3) then return end

	self.EnergyDamageEffects = (self.EnergyDamageEffects or 0) + 1

	timer.Simple(1.5, function(self)
		self.EnergyDamageEffects = self.EnergyDamageEffects - 1
	end, self)


	local ang = self.Entity:GetAngles()

	pos = pos or (self.Entity:LocalToWorld(self:OBBCenter()) + (ang:Up() * (self.Entity:BoundingRadius()/2)))

	local phys = self:GetPhysicsObject()

	if (!phys:IsValid()) then return end

	local mag = phys:GetVolume() / (phys:GetVolume() / 2) * (mag/10)

	self:EnergySparks((pos + (ang:Right() * mag)), mag)
	self:EnergySparks((pos - (ang:Right() * mag)), mag)
end

function meta:EnergySparks( pos, magnitude )
	local ent = ents.Create("point_tesla")
	ent:SetKeyValue("targetname", "teslab")
	ent:SetKeyValue("m_SoundName" ,"DoSpark")
	ent:SetKeyValue("texture" ,"sprites/physbeam.spr")
	ent:SetKeyValue("m_Color" ,"200 200 255")
	ent:SetKeyValue("m_flRadius" ,tostring(magnitude*80))
	ent:SetKeyValue("beamcount_min" ,tostring(math.ceil(magnitude)+4))
	ent:SetKeyValue("beamcount_max", tostring(math.ceil(magnitude)+12))
	ent:SetKeyValue("thick_min", tostring(magnitude))
	ent:SetKeyValue("thick_max", tostring(magnitude*8))
	ent:SetKeyValue("lifetime_min" ,"0.1")
	ent:SetKeyValue("lifetime_max", "0.2")
	ent:SetKeyValue("interval_min", "0.05")
	ent:SetKeyValue("interval_max" ,"0.08")
	ent:SetPos( pos )
	ent:Spawn()
	ent:Fire("DoSpark","",0)
	ent:Fire("kill","", 1)
end

